#include "StdAfx.h"
#include "ResultFile.h"
#include <sstream>
#include <cstdlib>
#include <iostream>

int PrintIntToBuffer( char* pBuffer , int nNum )
{
	int nPos = 0;
	bool bMinus = ( nNum < 0 );
	
	do
	{
		pBuffer[ nPos ] = '0' + (nNum % 10);
		nPos++;
	}while( (nNum = nNum / 10) != 0 );

	if( bMinus )
	{
		pBuffer[ nPos ] = '-';
		nPos++;
	}
	// reverse string
	for( int i = 0; i <= nPos/2-1; ++i )
	{
		char a = pBuffer[ i ];
		pBuffer[ i ] = pBuffer[ nPos - 1 - i ];
		pBuffer[ nPos - 1 - i ] = a;
	}
	pBuffer[ nPos ] = 0;
	return nPos;
}
ResultFile::ResultFile( )
{ 
	hFile = INVALID_HANDLE_VALUE;
	pBuffer = NULL;
	pCurrNumberBuffer = NULL;
}

int ResultFile::Init(long lLengthOfBuffer , wstring strFileName , int nOneNumberLength )
{
	nCurrNumberBufferLength = nOneNumberLength;
	hFile = CreateFile( strFileName.c_str(), GENERIC_WRITE, NULL , NULL , CREATE_ALWAYS , FILE_ATTRIBUTE_NORMAL  , NULL );
	if( hFile == INVALID_HANDLE_VALUE )
	{
		return FILE_SYSTEM_WRITING_ERROR;
	}
	lBufferLength = lLengthOfBuffer;
	pBuffer = (char*)malloc( lBufferLength*sizeof(char) );
	if( pBuffer == 0 )
	{
		CloseHandle (hFile );
		hFile = INVALID_HANDLE_VALUE;
		return MEMORY_ALLOCATION_ERROR;
	}
	
	pCurrNumberBuffer = (char*)malloc( lBufferLength*sizeof(char) );
	if( pCurrNumberBuffer == 0 )
	{
		free( pBuffer );
		pBuffer = NULL;
		CloseHandle (hFile );
		hFile = INVALID_HANDLE_VALUE;
		return MEMORY_ALLOCATION_ERROR;
	}
	lBufferPointer = 0;
	return 0;
}
int ResultFile::WriteNumber( int nNumber )
{

	//sprintf( pCurrNumberBuffer , "%d" , nNumber );
	long nCurrBufferLength = PrintIntToBuffer( pCurrNumberBuffer , nNumber );;
	long nCurrBufferPointer = 0;
	// Add an space to end
	pCurrNumberBuffer[ nCurrBufferLength ] = ' ';
	pCurrNumberBuffer[ nCurrBufferLength + 1 ] = 0;
	nCurrBufferLength++;

	// If record can't be placed to buffer, than write all we can, send buffer to file, and write again
	while( nCurrBufferLength - nCurrBufferPointer >= lBufferLength - lBufferPointer ) // Untill new record can be placed to buffer
	{
		// Write all we can
		memcpy( pBuffer + lBufferPointer, pCurrNumberBuffer + nCurrBufferPointer , lBufferLength - lBufferPointer );
		nCurrBufferPointer += lBufferLength - lBufferPointer;

		// Send to file, erase and prepare to new record
		DWORD lWriten;
		cout << "WriteToResultFile\n";
		WriteFile( hFile , pBuffer , lBufferLength*sizeof(char) , &lWriten , NULL );
		if( lWriten != lBufferLength*sizeof(char) )
		{
			CloseHandle( hFile );
			hFile = INVALID_HANDLE_VALUE;
			free( pBuffer );
			pBuffer = NULL;
			free( pCurrNumberBuffer );
			pCurrNumberBuffer = NULL;
			return FILE_SYSTEM_WRITING_ERROR;
		}
		lBufferPointer = 0;
	}

	//copy all to buffer
	memcpy( pBuffer + lBufferPointer , pCurrNumberBuffer + nCurrBufferPointer , nCurrBufferLength - nCurrBufferPointer );
	lBufferPointer += nCurrBufferLength - nCurrBufferPointer;


	return 0;
}

int ResultFile::CloseFile()
{
	DWORD lWriten;
	WriteFile( hFile , pBuffer , lBufferPointer*sizeof(char) , &lWriten , NULL );
	if( lWriten != lBufferPointer*sizeof(char) )
	{
		CloseHandle( hFile );
		hFile = INVALID_HANDLE_VALUE;
		free( pBuffer );
		pBuffer = NULL;
		free( pCurrNumberBuffer );
		pCurrNumberBuffer = NULL;
		return FILE_SYSTEM_WRITING_ERROR;
	}
	CloseHandle( hFile );
	hFile = INVALID_HANDLE_VALUE;
	free( pBuffer );
	pBuffer = NULL;
	free( pCurrNumberBuffer );
	pCurrNumberBuffer = NULL;
	return 0;
}
ResultFile::~ResultFile(void)
{
	if( hFile != INVALID_HANDLE_VALUE )
		CloseHandle( hFile );
	if( pBuffer != NULL )
		free( pBuffer );
	if( pCurrNumberBuffer != NULL )
		free( pCurrNumberBuffer );
}

